package com.a.lib_base.base.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.viewbinding.ViewBinding;
import com.a.lib_base.able.IBaseBarController;
import com.a.lib_base.databinding.ActivityBaseContainerBinding;

/**
 * 抽象的ViewBinding_Activity类，含有头部布局提供使用，默认不显示
 * BaseController 用来控制添加哪些复用控件，可以往里面添加
 *
 * @param <VB> ViewBinding类
 */
public abstract class BaseActivityViewBinding<VB extends ViewBinding> extends AppCompatActivity implements IBaseBarController {
    public VB b;
    public ActivityBaseContainerBinding baseContainerBinding;


    @Override
    public boolean isAddTitleBar() {
        return false;
    }

    @Override
    public String setTitle() {
        return null;
    }

    @Override
    public int setBarBackground() {
        return 0;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("TAG", "onCreate: " + isAddTitleBar());
        if (isAddTitleBar()) {
            baseContainerBinding = ActivityBaseContainerBinding.inflate(getLayoutInflater());
            baseContainerBinding.titleBar.imgBack.setOnClickListener(v -> finish());
            baseContainerBinding.titleBar.tvTitle.setText(setTitle());
            baseContainerBinding.titleBar.llCommonBar.setBackgroundColor(ContextCompat.getColor(this, setBarBackground()));
            b = viewBinding(baseContainerBinding.getRoot());
            setContentView(baseContainerBinding.getRoot());
        } else {
            b = viewBinding(null);
            setContentView(b.getRoot());
        }
    }


    public abstract VB viewBinding(ViewGroup view);


    public void ui(Runnable r) {
        runOnUiThread(r);
    }

    public void thread(Runnable r) {
        new Thread(r).start();
    }

}
